package gk.example.fragment;

import gk.example.App;

import java.util.Calendar;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

import com.example.gkdate.R;

public class ExtraDateInfoFragment extends Fragment implements OnClickListener {
	private Button mEtAnnounceDate;
	private Button mEtAnnounceTime;
	private Button mEtAnnouncePlace;
	private Button mEtAnnounceContact;
	private DatePickerDialog mDatePickerDialog;
	private TimePickerDialog mTimePickerDialog;
	private Calendar mCalendar;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.activity_date_info, container,
				false);
		mEtAnnounceDate = (Button) view.findViewById(R.id.btn_announce_date);
		mEtAnnounceTime = (Button) view.findViewById(R.id.btn_announce_time);
		mEtAnnouncePlace = (Button) view.findViewById(R.id.btn_announce_place);
		mEtAnnounceContact = (Button) view
				.findViewById(R.id.btn_announce_contact);
		mEtAnnounceContact.setText(App.BmobUser.getContact());
		mEtAnnounceDate.setOnClickListener(this);
		mEtAnnounceContact.setOnClickListener(this);
		mEtAnnouncePlace.setOnClickListener(this);
		mEtAnnounceTime.setOnClickListener(this);
		mCalendar = Calendar.getInstance();
		mDatePickerDialog = new DatePickerDialog(getActivity(), DateSet,
				mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH),
				mCalendar.get(Calendar.DAY_OF_MONTH));
		mTimePickerDialog = new TimePickerDialog(getActivity(), TimeSet,
				mCalendar.get(Calendar.HOUR_OF_DAY),
				mCalendar.get(Calendar.MINUTE), true);
		return view;
	}

	/**
	 * @description 日期设置匿名类
	 */
	DatePickerDialog.OnDateSetListener DateSet = new DatePickerDialog.OnDateSetListener() {

		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			// 每次保存设置的日期
			mCalendar.set(Calendar.YEAR, year);
			mCalendar.set(Calendar.MONTH, monthOfYear);
			mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			String date = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;
			mEtAnnounceDate.setText(date);

		}
	};
	TimePickerDialog.OnTimeSetListener TimeSet = new OnTimeSetListener() {

		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			// TODO Auto-generated method stub
			mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
			mCalendar.set(Calendar.MINUTE, minute);
			String time = hourOfDay + ":" + minute;
			mEtAnnounceTime.setText(time);
		}
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_announce_date:
			mDatePickerDialog.show();
			break;
		case R.id.btn_announce_time:
			mTimePickerDialog.show();
			break;
		case R.id.btn_announce_place:
			showResetDialog();
			break;
		default:
			break;
		}
	}

	/**
	 * 
	 * 描述： 显示选择地点对话框
	 * 
	 * @return void
	 */
	private void showResetDialog() {
		final EditText et = new EditText(getActivity());
		et.setHint("请输入地点");
		AlertDialog.Builder builder = new Builder(getActivity());
		builder.setView(et);
		builder.setTitle("选择地点");
		builder.setItems(new String[] { "神秘岛", "All blue" },
				new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub

					}
				});
		builder.setNegativeButton("确定",
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {

					}
				});
		builder.setPositiveButton("取消",
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();
	}
}

// private void announceDate() {
//
// String content = mEtAnnounceContent.getText().toString();
// String date = mEtAnnounceDate.getText().toString();
// String place = mEtAnnouncePlace.getText().toString();
// String contact = mEtAnnounceContact.getText().toString();
// String title = mEtAnnounceTitle.getText().toString();
// String time = mEtAnnounceTime.getText().toString();
// if (TextUtils.isEmpty(content) || TextUtils.isEmpty(date)
// || TextUtils.isEmpty(place) || TextUtils.isEmpty(contact)
// || TextUtils.isEmpty(title)) {
// T.show(this, "请完整地填写信息", Toast.LENGTH_SHORT);
// return;
// }
// mProgressDialog = new ProgressDialog();
// mProgressDialog.setMsg("发布中");
// mProgressDialog.show(getSupportFragmentManager(), "ANNOUNCE_TAG");
// mInfo = new DateInfo();
// mInfo.setNickName(App.BmobUser.getNickName());
// mInfo.setDateTitle(title);
// mInfo.setDateContent(content);
// mInfo.setDatePlace(place);
// mInfo.setDateDate(date);
// mInfo.setDateTime(time);
// mInfo.setDateContact(contact);
// mInfo.setUser(App.BmobUser);
// mInfo.save(this, new SaveListener() {
//
// @Override
// public void onSuccess() {
// // TODO Auto-generated method stub
// addRecordToUser(mInfo);
// Handler handler = new Handler();
// handler.postDelayed(new Runnable() {
//
// @Override
// public void run() {
// // TODO Auto-generated method stub
// finish();
// mProgressDialog.dismiss();
// T.show(AnnounceActivity.this, "发布成功",
// Toast.LENGTH_SHORT);
// startActivity(new Intent(AnnounceActivity.this,
// MainActivity.class));
// }
// }, 1500);
//
// }
//
// @Override
// public void onFailure(int arg0, String arg1) {
// // TODO Auto-generated method stub
//
// }
// });
//
// }
//
// private void addRecordToUser(DateInfo dateInfo) {
// BmobRelation record = new BmobRelation();
// record.add(dateInfo);
// App.BmobUser.setDateRecord(record);
// App.BmobUser.update(this, new UpdateListener() {
//
// @Override
// public void onSuccess() {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void onFailure(int arg0, String arg1) {
// // TODO Auto-generated method stub
//
// }
// });
// }
